﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for DA_User
/// </summary>
public class DA_User
{
    AuditBillingEntities objABEnt;
    public DA_User()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public PLMstUser DAValidateUserLogin(PLMstUser pObjUser)
    {
        using (objABEnt = new AuditBillingEntities())
        {
            PLMstUser objUser = new PLMstUser();
            var objRes = (from u in objABEnt.MstUsers
                          where u.UserEmail == pObjUser.UserEmail && u.UserLoginPassword == pObjUser.UserLoginPassword && u.IsActive == true
                          select u
                              ).FirstOrDefault();
            if (objRes == null)
            {
                objUser.IsLoginSuccess = false;
            }
            else
            {
                objUser.MstUserID = objRes.MstUserID;
                objUser.UserFullName = objRes.UserFirstName + " " + (objRes.UserLastName ?? "");
                objUser.UserEmail = objRes.UserEmail;
                objUser.IsAdmin = objRes.IsAdmin;
                objUser.IsLoginSuccess = true;
            }
            LoginHistory objLoginHis = new LoginHistory();
            objLoginHis.UserID = (objRes == null ? 0 : objRes.MstUserID);
            objLoginHis.UserEmail = pObjUser.UserEmail;
            objLoginHis.UserPass = pObjUser.UserLoginPassword;
            objLoginHis.CreatedOn = DateTime.Now;
            objLoginHis.LoginFlag = (objUser == null ? "Login Failed" : "Success");

            objABEnt.LoginHistories.Add(objLoginHis);
            objABEnt.SaveChanges();
            return objUser;
        }
    }

    public List<PLMstFormMasters> DA_fnGetEmployeeMenu(PLMstUser pObjUser)
    {
        using (objABEnt = new AuditBillingEntities())
        {
            var varRes = (from f in objABEnt.MstFormMasters
                          join m in objABEnt.TrnFormUserMappings on f.MstFormMasterID equals m.FormID
                          into LO_m
                          from m in LO_m.DefaultIfEmpty()
                          where f.IsActive == true && m.MstUserID == pObjUser.MstUserID
                          orderby f.ParentForm, f.SortOrder
                          select new PLMstFormMasters()
                          {
                              FormID = f.MstFormMasterID,
                              FormName = f.FormName,
                              ParentForm = f.ParentForm,
                              SortOrder = f.SortOrder,
                              FormURL = f.FormURL,
                              ViewAccess = (m == null ? false : m.ViewAccess),
                              EditAccess = (m == null ? false : m.AddEditAccess),
                              GlobalAccess = (m == null ? false : m.GlobalAccess)
                          }).Distinct().ToList();
            return varRes;

        }
    }
    public PLMstUser DA_fnGetUserByEmailID(PLMstUser pObjUser)
    {
        using (objABEnt = new AuditBillingEntities())
        {
            PLMstUser objUser = new PLMstUser();
            var objRes = (from u in objABEnt.MstUsers
                          where u.UserEmail == pObjUser.UserEmail && u.IsActive == true
                          select u
                              ).FirstOrDefault();
            if (objRes == null)
            {
                objUser.IsLoginSuccess = false;

            }
            else
            {
                objUser.MstUserID = objRes.MstUserID;
                objUser.UserFullName = objRes.UserFirstName + " " + (objRes.UserLastName ?? "");
                objUser.UserEmail = objRes.UserEmail;
                objUser.IsAdmin = objRes.IsAdmin;
                objUser.IsLoginSuccess = true;
            }
            return objUser;
        }
    }

    public List<PLMstUser> DA_fnGetEmployeeList(PLMstUser pObjUser)
    {
        using (objABEnt = new AuditBillingEntities())
        {
            var varRes = (from u in objABEnt.MstUsers
                          where (pObjUser.SearchIsActive == null || u.IsActive == pObjUser.SearchIsActive) &&
                                (pObjUser.UserID == null || pObjUser.UserID == u.MstUserID) &&
                                (string.IsNullOrEmpty(pObjUser.UserFullName) || u.UserFullName.ToLower().Contains(pObjUser.UserFullName.ToLower())) &&
                                (u.IsAdmin ?? false) == false
                          orderby u.UserFullName
                          select new PLMstUser()
                          {
                              UserID = u.MstUserID,
                              UserFirstName = u.UserFirstName,
                              UserLastName = u.UserLastName,
                              UserFullName = u.UserFullName,
                              IsActive = u.IsActive,
                              UserEmail = u.UserEmail,
                              UserLoginPassword = u.UserLoginPassword
                          });
            return varRes.ToList();

        }
    }


    public int DA_InsertUpdateUser(PLMstUser pObjUser)
    {
        using (objABEnt = new AuditBillingEntities())
        {
            MstUser objUser = new MstUser();
            if (pObjUser.UserID.GetValueOrDefault() != 0)
            {
                objUser = (from u in objABEnt.MstUsers
                           where u.MstUserID == pObjUser.UserID
                           select u).ToList().FirstOrDefault();
            }
            objUser.UserFirstName = pObjUser.UserFirstName;
            objUser.UserLastName = pObjUser.UserLastName;
            objUser.UserFullName = pObjUser.UserFirstName + " " + pObjUser.UserLastName;
            objUser.UserEmail = pObjUser.UserEmail;
            objUser.UserLoginPassword = pObjUser.UserLoginPassword;
            objUser.IsActive = pObjUser.IsActive;

            if (pObjUser.UserID.GetValueOrDefault() != 0)
            {
                objUser.ModifiedBy = SessionInfo.prop_getObj.EmployeeID;
                objUser.ModifiedOn = DateTime.Now;
            }
            else
            { 
                objUser.CreatedBy = SessionInfo.prop_getObj.EmployeeID;
                objUser.CreatedOn = DateTime.Now;
                objABEnt.MstUsers.Add(objUser);
            }
            objABEnt.SaveChanges();
            return objUser.MstUserID;
        }
    }

}